﻿namespace Lib.WPF.Core;
/// <summary>
/// 内存数据存储
/// </summary>
public static class Session {
    /// <summary>
    /// 静态构造函数
    /// </summary>
    static Session() {
        Initialize();
    }
    /// <summary>
    /// 
    /// </summary>
    public static Dictionary<string, object> DATA = [];
    /// <summary>
    /// 初始化
    /// </summary>
    public static void Initialize() { }
    /// <summary>
    /// 
    /// </summary>
    public static bool ContainsKey(string Key) {
        return DATA.ContainsKey(Key);
    }
    /// <summary>
    /// 保存数据, 如果已存在则更新数据
    /// </summary>
    public static bool Set<T>(string Key, T Data) {
        if (DATA.ContainsKey(Key)) {
            DATA[Key] = Data;
        } else {
            DATA.Add(Key, Data);
        }
        return true;
    }
    /// <summary>
    /// 查找数据
    /// </summary>
    public static T Get<T>(string Key) {
        if (DATA.ContainsKey(Key)) {
            if (DATA[Key] is T Data) {
                return Data;
            }
        }
        return default;
    }
    /// <summary>
    /// 删除数据 
    /// </summary>
    public static bool Remove<T>(string Key) {
        return DATA.Remove(Key);
    }
    /// <summary>
    /// 释放资源
    /// </summary>
    public static void Dispose() {
        DATA.Clear();
    }
}